from rest_framework import serializers

from backend.cews.models import (Asset, Component, Equipment,
                                 EquipmentLevelRelation, Imte, Parts, PartsEDoc,
                                 PartsFitmentLog, PartsReplacement,
                                 PartVersion, RepairAction, RepairOrder,
                                 RepairOrderAction,
                                 RepairOrderActionLogEquipment,
                                 RepairOrderEDoc, RepairOrderInspection,
                                 RepairOrderPart, RepairOrderPartReplacement,
                                 RepairOrderPartStatusLog,
                                 RepairOrderPartVersion,
                                 RepairOrderRepairDetails, RepairOrderTask,
                                 RepairOrderTaskImte, Status, StockItem,
                                 StockItemEDoc, StockItemRelation,
                                 StockItemReplacement, StockItemTask, System,
                                 Task, VersionMaster, Workgroup, Log,
                                 TrainborneMaintenanceRecords, QRMap, QRMapLocation,
                                 QRMapLocationCategory, StockItemImprovementWork,
                                 PartsImprovementWork, RepairOrderImprovementWork)

from django.contrib.auth import get_user_model

User = get_user_model()

class AssetSerializer(serializers.ModelSerializer):
    class Meta:
        model = Asset
        fields = '__all__'


class ComponentSerializer(serializers.ModelSerializer):
    class Meta:
        model = Component
        fields = '__all__'


class PartsSerializer(serializers.ModelSerializer):
    is_parent = serializers.ReadOnlyField()
    class Meta:
        model = Parts
        fields = '__all__'
        extra_fields = ['is_parent']


class PartsFitmentLogSerializer(serializers.ModelSerializer):
    class Meta:
        model = PartsFitmentLog
        fields = '__all__'


class PartsReplacementSerializer(serializers.ModelSerializer):
    class Meta:
        model = PartsReplacement
        fields = '__all__'


class PartVersionSerializer(serializers.ModelSerializer):
    parts = PartsSerializer(many=False)

    class Meta:
        model = PartVersion
        fields = '__all__'


class RepairActionSerializer(serializers.ModelSerializer):
    class Meta:
        model = RepairAction
        fields = '__all__'


class RepairOrderSerializer(serializers.ModelSerializer):
    order_no = serializers.ReadOnlyField()

    complete_by_username = serializers.SerializerMethodField("get_username")
    def get_username(self, obj):
        if obj.complete_by is not None:
            return obj.complete_by.username
        return obj.complete_by
    class Meta:
        model = RepairOrder
        fields = '__all__'
        extra_fields = ['order_no', 'complete_by_username']


class RepairOrderActionSerializer(serializers.ModelSerializer):
    class Meta:
        model = RepairOrderAction
        fields = '__all__'


class RepairOrderActionLogEquipmentSerializer(serializers.ModelSerializer):
    class Meta:
        model = RepairOrderActionLogEquipment
        fields = '__all__'


class RepairOrderEDocSerializer(serializers.ModelSerializer):
    parts = PartsSerializer(many=False)
    class Meta:
        model = RepairOrderEDoc
        fields = '__all__'


class RepairOrderInspectionSerializer(serializers.ModelSerializer):
    class Meta:
        model = RepairOrderInspection
        fields = '__all__'


class RepairOrderPartSerializer(serializers.ModelSerializer):
    part = PartsSerializer(many=False)
    class Meta:
        model = RepairOrderPart
        fields = '__all__'


class OrderPartSearchSerializer(serializers.ModelSerializer):
    # order_no = serializers.ReadOnlyField()
    repair_order = RepairOrderSerializer(many=False)

    class Meta:
        model = RepairOrderPart
        fields = ('id', 'stock_code', 'serial_no', 'repair_order', 'description')


class RepairOrderPartReplacementSerializer(serializers.ModelSerializer):
    class Meta:
        model = RepairOrderPartReplacement
        fields = '__all__'


class RepairOrderPartStatusLogSerializer(serializers.ModelSerializer):
    class Meta:
        model = RepairOrderPartStatusLog
        fields = '__all__'


class RepairOrderPartVersionSerializer(serializers.ModelSerializer):
    repair_order_part = RepairOrderPartSerializer(many=False)

    class Meta:
        model = RepairOrderPartVersion
        fields = '__all__'


class RepairOrderRepairDetailsSerializer(serializers.ModelSerializer):
    class Meta:
        model = RepairOrderRepairDetails
        fields = '__all__'
        extra_fields = ['current_user']


class StatusSerializer(serializers.ModelSerializer):
    class Meta:
        model = Status
        fields = '__all__'


class StockItemSerializer(serializers.ModelSerializer):
    is_parent = serializers.ReadOnlyField()

    class Meta:
        model = StockItem
        fields = '__all__'


class StockItemFKSerializer(serializers.ModelSerializer):
    class Meta:
        model = StockItem
        fields = [
            'id',
            'stock_code',
        ]


class StockItemRelationSerializer(serializers.ModelSerializer):
    child = StockItemSerializer(many=False)

    class Meta:
        model = StockItemRelation
        fields = [
            'child',
            'sequence',
        ]


class StockItemEDocSerializer(serializers.ModelSerializer):
    class Meta:
        model = StockItemEDoc
        fields = '__all__'

class StockItemEDocSerializerWithDescription(serializers.ModelSerializer):
    stock_item = StockItemSerializer(many=False)
    class Meta:
        model = StockItemEDoc
        fields = '__all__'

class StockItemReplacementSerializer(serializers.ModelSerializer):
    stock_item = StockItemSerializer(many=False)

    class Meta:
        model = StockItemReplacement
        fields = '__all__'
        extra_fields = ['is_parent']


class StockItemTaskEformSerializer(serializers.ModelSerializer):
    stock_item = StockItemFKSerializer(many=False)
    has_eform = serializers.ReadOnlyField()

    class Meta:
        model = StockItemTask
        fields = '__all__'


class StockItemTaskSerializerMain(serializers.ModelSerializer):
    class Meta:
        model = StockItemTask
        fields = '__all__'


class SystemSerializer(serializers.ModelSerializer):
    class Meta:
        model = System
        fields = '__all__'


class WorkgroupSerializer(serializers.ModelSerializer):
    class Meta:
        model = Workgroup
        fields = '__all__'


class TaskSerializer(serializers.ModelSerializer):
    class Meta:
        model = Task
        fields = '__all__'


class VersionMasterSerializer(serializers.ModelSerializer):
    description = serializers.ReadOnlyField()
    equipment = serializers.ReadOnlyField()
    class Meta:
        model = VersionMaster
        fields = '__all__'
        extra_fields = ['description','equipment']


class EquipmentSerializer(serializers.ModelSerializer):
    class Meta:
        model = Equipment
        fields = '__all__'


class EquipmentLevelRelationSerializer(serializers.ModelSerializer):
    equipment_class = serializers.ReadOnlyField()

    class Meta:
        model = EquipmentLevelRelation
        fields = '__all__'
        extra_fields = ['equipment_class']


class ImteSerializer(serializers.ModelSerializer):
    class Meta:
        model = Imte
        fields = '__all__'


class RepairOrderTaskImteSerializer(serializers.ModelSerializer):
    class Meta:
        model = RepairOrderTaskImte
        fields = '__all__'


class RepairOrderMaintenanceHistoryChildSerializer(
    serializers.ModelSerializer
):
    repair_order = RepairOrderSerializer(many=False)

    class Meta:
        model = RepairOrderPart
        fields = [
            "stock_code",
            "status",
            "repair_order",
        ]


class RepairOrderMaintenanceHistorySerializer(serializers.Serializer):
    serial_no = serializers.ReadOnlyField()
    maintenance_history = \
        RepairOrderMaintenanceHistoryChildSerializer(many=True)

    class Meta:
        fields = [
            "serial_no",
            "maintenance_history",
        ]


class VersionCurrentLatestSerailizer(serializers.Serializer):
    repair_order_part_version_id = serializers.ReadOnlyField()
    repair_order_part_id = serializers.ReadOnlyField()
    stock_code = serializers.ReadOnlyField()
    serail_no = serializers.ReadOnlyField()
    current_sw_ver = serializers.ReadOnlyField()
    current_hw_ver = serializers.ReadOnlyField()
    latest_sw_ver = serializers.ReadOnlyField()
    latest_hw_ver = serializers.ReadOnlyField()
    sw_ver_correct = serializers.BooleanField(read_only=True)
    hw_ver_correct = serializers.BooleanField(read_only=True)

    class Meta:
        fields = [
            'repair_order_part_id',
            'stock_code',
            'serail_no',
            "current_sw_ver",
            "current_hw_ver",
            "latest_sw_ver",
            "latest_hw_ver",
        ]


class DepotCarPartsSerailizer(serializers.Serializer):
    stock_code = serializers.ReadOnlyField()
    serial_no = serializers.ReadOnlyField()
    car = serializers.ReadOnlyField()
    child = serializers.DictField(
        child=serializers.DictField(
            child=serializers.CharField()
        )
    )

    class Meta:
        fields = [
            'stock_code',
            'serial_no',
            'car',
            "child",
            'sw_ver',
            'hw_ver'
        ]

class UserBasicSerializer(serializers.ModelSerializer):
    
    class Meta:
        model = User
        fields = ['username', 'id', 'email']
    

class RepairOrderTaskSerializer(serializers.ModelSerializer):
    
    has_eform = serializers.BooleanField(read_only=True)
    is_eform_created = serializers.BooleanField(read_only=True)
    stock_item_task = StockItemTaskSerializerMain(many=False, read_only=True)
    
    done_by = UserBasicSerializer(many=False, read_only=True)
    verify_by = UserBasicSerializer(many=False, read_only=True)
    endorse_by = UserBasicSerializer(many=False, read_only=True)
    verify_user = UserBasicSerializer(many=False, read_only=True)
    endorse_user = UserBasicSerializer(many=False, read_only=True)
    
    class Meta:
        model = RepairOrderTask
        fields = '__all__'
        extra_fields = ['has_eform', 'is_eform_created']


class PartsHardTimeReplacementSerializer(serializers.Serializer):
    id = serializers.ReadOnlyField()
    stock_code = serializers.ReadOnlyField()
    serial_no = serializers.ReadOnlyField()
    description = serializers.ReadOnlyField()
    component = serializers.ReadOnlyField()
    last_replaced = serializers.DateTimeField()
    next_replace = serializers.DateTimeField()
    one_time = serializers.ReadOnlyField()
    period = serializers.ReadOnlyField()
    diff = serializers.ReadOnlyField()
    is_expired = serializers.ReadOnlyField()
    is_critical = serializers.ReadOnlyField()

    class Meta:
        fields = [
            'id',
            'stock_code',
            'description',
            'serial_no',
            'component',
            'last_replaced',
            'next_replace',
            'one_time',
            'period',
            'diff',
            'is_expired',
            'is_critical'
        ]


class RepairOrderPartsHardTimeReplacementSerializer(serializers.Serializer):
    id = serializers.ReadOnlyField()
    stock_code = serializers.ReadOnlyField()
    description = serializers.ReadOnlyField()
    serial_no = serializers.ReadOnlyField()
    component = serializers.ReadOnlyField()
    last_replaced = serializers.DateTimeField()
    next_replace = serializers.DateTimeField()
    one_time = serializers.ReadOnlyField()
    period = serializers.ReadOnlyField()
    replaced = serializers.ReadOnlyField()
    complete = serializers.ReadOnlyField()
    order_only = serializers.ReadOnlyField()
    diff = serializers.ReadOnlyField()
    is_expired = serializers.ReadOnlyField()
    is_critical = serializers.ReadOnlyField()
    deleted = serializers.ReadOnlyField()

    class Meta:
        fields = [
            'id',
            'stock_code',
            'description'
            'serial_no'
            'component',
            'last_replaced',
            'next_replace',
            'one_time',
            'period',
            'replaced',
            'complete',
            'order_only',
            'diff',
            'is_expired',
            'is_critical',
            'deleted'
        ]


class PartsEDocSerializer(serializers.ModelSerializer):
    class Meta:
        model = PartsEDoc
        fields = '__all__'

class PartsWithChildSerializer(serializers.ModelSerializer):
    card_installed = serializers.ReadOnlyField()
    parent_stock_code = serializers.ReadOnlyField()
    parent_serial_no = serializers.ReadOnlyField()
    parent_short_description = serializers.ReadOnlyField()
    class Meta:
        model = Parts
        fields = '__all__'
        extra_fields = ['card_installed','parent_stock_code','parent_serial_no', 'parent_short_description']

class PartsWithFitmentSerializers(serializers.ModelSerializer):
    class Meta:
        model = Parts
        fields = ['id', 'stock_code', 'serial_no', 'description']

class StockItemWithChildSerializer(serializers.ModelSerializer):
    assigned = serializers.ReadOnlyField()
    edoc_completion_rate = serializers.ReadOnlyField()
    class Meta:
        model = StockItem
        fields = '__all__'
        extra_fields = ['assigned', 'edoc_completion_rate']
class StockItemObsolescenceSerializer(serializers.ModelSerializer):
    class Meta:
        model = StockItem
        fields = ["id", "stock_code", "description"]

class LogSerializer(serializers.ModelSerializer):
    order_no = serializers.ReadOnlyField()

    class Meta:
        model = Log
        fields = '__all__'
        extra_fields = ['order_no']

class StockCodeSerializer(serializers.ModelSerializer):
    class Meta:
        model = StockItem
        fields = ['id','stock_code','description']


class TrainborneMaintenanceRecordsSerializer(serializers.ModelSerializer):
    class Meta:
        model = TrainborneMaintenanceRecords
        fields = '__all__'


class QRMapSerializer(serializers.ModelSerializer):
    class Meta:
        model = QRMap
        fields = '__all__'

class QRMapLocationSerializer(serializers.ModelSerializer):
    class Meta:
        model = QRMapLocation
        fields = '__all__'

class QRMapLocationCategorySerializer(serializers.ModelSerializer):
    class Meta:
        model = QRMapLocationCategory
        fields = '__all__'

class StockItemImpWorkSerializer(serializers.ModelSerializer):
    class Meta:
        model = StockItemImprovementWork
        fields = '__all__'
class StockItemImpWithDescriptionWorkSerializer(serializers.ModelSerializer):
    stock_item = StockItemSerializer(many=False)
    class Meta:
        model = StockItemImprovementWork
        fields = '__all__'

class PartsImpWorkSerializer(serializers.ModelSerializer):
    class Meta:
        model = PartsImprovementWork
        fields = '__all__'

class RepairOrderImpWorkSerializer(serializers.ModelSerializer):
    parts = PartsSerializer(many=False)
    class Meta:
        model = RepairOrderImprovementWork
        fields = '__all__'